<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sessions extends MX_Controller {
	
	function __construct(){
		parent::__construct();
		
		$this->load->model('setting/model_setting');
		
		$setting = $this->model_setting->get();
		foreach($setting->result_array() as $row){
			$this->config->set_item($row['name'], $row['value']);
		}
	}

    function login() {

        $this->load->model('model_auth');
		$mdl_group = $this->load->model('group/model_group');
		
        if (isset($_POST['username'])) {
			
			$user = $this->model_auth->login($_POST['username'],$_POST['password']);
			if($user)
			{
				$id = $this->session->userdata('user_id');
				$log['activity']='LOGIN';
				$log['information']='Berhasil Login untuk user:'.$id;
				$this->model_log->add($log);
				
				
				$respon = array();
				$respon['status'] = "success";
				$respon['msg'] = "";

				$respon['redirect'] = base_url()."welcome";
				
				echo json_encode($respon);
				
				return;
			}else{
				if($this->session->userdata('switch') == TRUE){
					$id = $this->session->userdata('user_id');
					$query = $this->model_auth->get_user($id);
					$username = "";
					$ip = "0.0.0.0";
					foreach($query->result_array() as $row){
						$username = $row['username'];
						$ip = $row['ip_address'];
					}
					$msg = "<br />1. Username $username sudah login dan aktif dari host $ip.<br /><br />2. Anda dapat meneruskan untuk login dengan mematikan session login di host $ip tsb.<br /><br />3. Pastikan host tsb tidak sedang melakukan transaksi ketika Anda mematikan session login-nya.";
					$this->session->set_flashdata('error', $msg);
					redirect("sessions/switchUser");
				}else{
					$respon = array();
					$respon['status'] = "error";
					$respon['msg'] = "Username atau password tidak ditemukan.";
					
					echo json_encode($respon);
					return;
				}
			}
        }
		$data['group'] = $mdl_group->get_group();
		// if($this->session->userdata('login') === TRUE){
			// $this->model_auth->logout();
		// }
		
		$params['content'] = $this->load->view('login','',TRUE);
        $params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['footer']['js_init'] = $this->load->view('js_init','',TRUE);
		
		echo modules::run('template/loginpage', $params);

    }
	
	function switchUser(){
		$this->load->model('model_auth');
		
		// if($this->session->userdata('login') === TRUE){
			// redirect(base_url());
		// }
		
		if(isset($_POST['code'])){
			if(isset($_POST['confirm'])){
				// $this->load->library('curl');
				
				// $postdata['privatekey'] = "6LfYWN4SAAAAAGTGJ8h6DSoZOrkHeBZlM-eaZ5uC";
				// $postdata['remoteip'] = $this->input->ip_address();
				// $postdata['challenge '] = $_POST['recaptcha_challenge_field'];
				// $postdata['response '] = $_POST['recaptcha_response_field'];
				
				// $url = "http://www.google.com/recaptcha/api/verify";
				// $this->curl->create($url);
				// $this->curl->option(CURLOPT_TIMEOUT, 40);
				// $this->curl->option(CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)");
				// $this->curl->post($postdata);
				// echo $html = $this->curl->execute();
				// //$this->curl->debug();
				// $info = $this->curl->info;
				// var_dump($_POST);
				
				if($this->session->userdata('word') == $_POST['code']){
					$this->model_auth->switch_session();
					
					redirect(base_url());
				}else{
					$this->session->set_flashdata('error', '<br />Kode verifikasi yang Anda masukkan salah.');
					redirect("sessions/switchUser");
				}
			}else{
				$this->session->set_flashdata('error', '<br />Anda belum menyetujui untuk mematikan session.');
				redirect("sessions/switchUser");
			}
		}
		
		$data['image'] = $this->captcha();
		$params['content'] = $this->load->view('switch',$data,TRUE);
        $params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		
		echo modules::run('template/loginpage', $params);
	}
	
	function captcha(){
		$this->load->helper('captcha');
		$this->load->helper('string');
		
		$word = random_string('numeric', 6);
		$vals = array(
			'word'		 => $word,
			'img_path'     => './captcha/',
			'img_url'     => base_url().'captcha/',
			'font_path'	 => './font/cheri.ttf',
			'img_width'     => '150',
			'img_height' => 40,
			'border' => 0, 
			'expiration' => 20
		);
	
		 // create captcha image
		$cap = create_captcha($vals);
		// echo $cap['time'];
		// echo $cap['word'];
		
		$this->session->set_userdata("word",$cap['word']);
		
		return $cap['image'];
	}

    function logout() {
		
		if($this->session->userdata('login') === TRUE){
			$this->load->model('model_auth');
			
			$this->model_auth->logout();
		}

        redirect('sessions/login');

    }
}